﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Sharp3DMath.Core;

namespace Sharp3DMath.Strokes
{
    public class FileDataTools
    {
        public static Vector2F[] ReadDataToArray(string path)
        {
            try
            {
                return ReadDataToList(path).ToArray();
            }
            catch (IOException e)
            {
                throw e;
            }
           
        }
        public static List<Vector2F> ReadDataToList(string path)
        {
            StreamReader reader;
            try
            {
                reader = File.OpenText(path);
                List<Vector2F> list = new List<Vector2F>(100);
                string s = "";
                while ((s = reader.ReadLine()) != null)
                {
                    int splite = s.IndexOf(' ');
                    string sub1 = s.Substring(0, splite);
                    string sub2 = s.Substring(splite + 1);
                    list.Add(new Vector2F(float.Parse(sub1), float.Parse(sub2)));
                }
                reader.Close();
                return list;
            }
            catch (IOException e)
            {
                throw e;
            }
        }

        public static Stroke ReadDataToStroke(string path)
        {
            return new Stroke(ReadDataToList(path));
        }

        /// <summary>
        /// just save points,no time,no speed,no direction...
        /// </summary>
        /// <param name="path"></param>
        public static void SaveStrokePoints(string path, Stroke stroke)
        {
 
        }
        /// <summary>
        /// just save stroke's points information
        /// </summary>
        /// <param name="path"></param>
        /// <param name="stroke"></param>
        public static void SavePoints(string path,Stroke stroke)
        {
            if (stroke.Points.Count <= 0)
            {
                return;
            }
            StreamWriter writer = File.CreateText(path);

            foreach (Vector2F item in stroke.Points)
            {
                writer.WriteLine(item.X + " " + item.Y);
            }
            writer.Flush();
            writer.Close();
        }
    }
}
